]>
Commit | Line | Data |
---|---|---|
1 | import SwiftUI | |
2 | ||
3 | struct MapEdges: View { | |
4 | ||
5 | let mapSize: CGSize | |
6 | let lineWidth: CGFloat | |
7 | let vertexSize: CGSize | |
8 | let edges: [MapEdge] | |
9 | ||
10 | let arrowheadSize = CGFloat(10.0) | |
11 | ||
12 | var body: some View { | |
13 | ForEach(edges, id: \.id) { edge in | |
14 | Path { path in | |
15 | ||
16 | // First we transform edges from percentage to map coordinates | |
17 | let origin = CGPoint(x: w(edge.origin.x), y: h(edge.origin.y)) | |
18 | let destination = CGPoint(x: w(edge.destination.x), y: h(edge.destination.y)) | |
19 | ||
20 | let slope = (destination.y - origin.y) / (destination.x - origin.x) | |
21 | let angle = atan(slope) | |
22 | let multiplier = CGFloat(slope < 0 ? -1.0 : 1.0) | |
23 | let upperAngle = angle - CGFloat.pi / 4.0 | |
24 | let lowerAngle = angle + CGFloat.pi / 4.0 | |
25 | ||
26 | let offsetOrigin = CGPoint( | |
27 | x: origin.x + multiplier * (vertexSize.width / 2.0) * cos(angle), | |
28 | y: origin.y + multiplier * (vertexSize.height / 2.0) * sin(angle)) | |
29 | let offsetDestination = CGPoint( | |
30 | x: destination.x - multiplier * (vertexSize.width / 2.0) * cos(angle), | |
31 | y: destination.y - multiplier * (vertexSize.height / 2.0) * sin(angle)) | |
32 | ||
33 | path.move(to: offsetOrigin) | |
34 | path.addLine(to: offsetDestination) | |
35 | ||
36 | if edge.arrowhead { | |
37 | path.move(to: offsetDestination) | |
38 | path.addLine( | |
39 | to: CGPoint( | |
40 | x: offsetDestination.x - multiplier * arrowheadSize * cos(upperAngle), | |
41 | y: | |
42 | offsetDestination.y - multiplier * arrowheadSize * sin(upperAngle))) | |
43 | ||
44 | path.move(to: offsetDestination) | |
45 | path.addLine( | |
46 | to: CGPoint( | |
47 | x: offsetDestination.x - multiplier * arrowheadSize * cos(lowerAngle), | |
48 | y: | |
49 | offsetDestination.y - multiplier * arrowheadSize * sin(lowerAngle))) | |
50 | } | |
51 | path.move(to: offsetDestination) | |
52 | path.closeSubpath() | |
53 | }.applying( | |
54 | CGAffineTransform(translationX: vertexSize.width / 2.0, y: vertexSize.height / 2.0) | |
55 | ).stroke(Color.map.vertexColor, lineWidth: lineWidth) | |
56 | } | |
57 | } | |
58 | ||
59 | func h(_ dimension: CGFloat) -> CGFloat { | |
60 | max(0.0, min(mapSize.height, dimension * mapSize.height / 100.0)) | |
61 | } | |
62 | ||
63 | func w(_ dimension: CGFloat) -> CGFloat { | |
64 | max(0.0, min(mapSize.width, dimension * mapSize.width / 100.0)) | |
65 | } | |
66 | } | |
67 | ||
68 | struct MapEdges_Previews: PreviewProvider { | |
69 | static var previews: some View { | |
70 | MapEdges( | |
71 | mapSize: CGSize(width: 400.0, height: 400.0), lineWidth: 1.0, | |
72 | vertexSize: CGSize(width: 25.0, height: 25.0), | |
73 | edges: [ | |
74 | MapEdge( | |
75 | id: 1, origin: CGPoint(x: 2.0, y: 34.0), destination: CGPoint(x: 23.0, y: 76.2), | |
76 | arrowhead: true) | |
77 | ]) | |
78 | } | |
79 | } |